const mongoServer = require("./src/customtrade.js").mongoServer;

class CustomTrade {
  constructor(ctx) {
    ctx = ctx || {};
    if (!ctx && !ctx.deployment) {
      throw new Error('deployment is need!');
    }
    this.deployment = ctx.deployment;
    this.context = ctx;
  }

  /**
   * @description: 添加订单
   *  @param openId  密文id 必填
   *  @param itemId  必填 
   *  @param  custominfo 必填
   *  @param  orderStatus 默认值为null 初始状态0
   * @return  orderId   返回订单id
   */
  async addOrderInfo(obj) {
    obj = obj || {};
    if (!obj.openId || !obj.itemId) {
      return {
        success: false,
        data: {},
        errorCode: "422",
        errorMessage: "openId&&itemId must be needed!"
      }
    }
    return await mongoServer.addOrderInfo(this.context,obj);
  }

  /**
   * @description: 查询订单信息
   * @param  order_id 订单id 必填
   * @return 订单信息 
   */
  async getOrderInfo(obj) {
    obj = obj || {};
    if (!obj._id) {
      return {
        success: false,
        data: {},
        errorCode: "422",
        errorMessage: "_id must be needed!"
      }
    }

    return await mongoServer.getOrderInfo(this.context,obj);
  }
  
  /**
   * @description: 根据id修改订单状态
   * @param  order_id 订单id 必填
   * @return 订单信息 
   */
  async updateOrderInfo(obj) {
    obj = obj || {};
    // return obj;
    if (!obj._id) {
      return {
        success: false,
        data: {},
        errorCode: "422",
        errorMessage: "_id must be needed!"
      }
    }

    return await mongoServer.updateOrderInfo(this.context,obj);
  }

   /**
   * @description: 定时任务 修改订单信息，即已经生成一个订单 ，多久一次？  测试为1分钟，使用待定
   */
  async updateOrderInfoByTime() {
    return await mongoServer.updateOrderInfoByTime(this.context);
  }
}

module.exports = {
  CustomTrade,
};
